package com.cdel.akka.AkkainJAVA.demo3router;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.routing.RoundRobinRoutingLogic;
import akka.routing.Router;

/**
 * 这种方式是通过AKKA提供的API,手动的创建Router对象,然后调用addRoutee方法手动的添加Actor
 * (需要注意,每一次调用addRoutee都会返回一个新的Router对象),然后通过route来发送消息.
 *
 * @author DELL
 *
 */
public class ControlActor extends UntypedActor {

	Router router;
	{
		Props props = Props.create(WriterActor.class).withDispatcher("writer-dispatcher");
		router = new Router(new RoundRobinRoutingLogic());
		for (int i = 0; i < 3; i++) {
			ActorRef actor = getContext().actorOf(props, "writer_"+ i);
			router = router.addRoutee(actor);
			//需要注意,需要接收addRoutee的返回
		}
	}

	@Override
	public void onReceive(Object message) throws Exception {
		if (message instanceof StartCommand) {
			router.route("Insert" + ((StartCommand) message).getActorCount(), ActorRef.noSender());
		}
	}

}